﻿using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using Tarantino.Core.Commons.Services.Repositories;
using Tarantino.Core.Commons.Services.Security;
using Tarantino.Samples.Core.Model;
using Tarantino.Samples.Core.Services.Security;
using Tarantino.Samples.Core.Services.Security.Impl;

namespace Tarantino.Samples.UnitTests.Services.Security
{
	[TestFixture]
	public class NewUserServiceTester
	{
		[Test]
		public void Adds_new_user_and_performs_login()
		{
			SystemUser user = new SystemUser();

		  MockRepository mocks = new MockRepository();
		  ILoginService loginService = mocks.CreateMock<ILoginService>();
			ISystemUserFactory factory = mocks.CreateMock<ISystemUserFactory>();
			IPersistentObjectRepository repository = mocks.CreateMock<IPersistentObjectRepository>();
			ISystemUserRepository userRepository = mocks.CreateMock<ISystemUserRepository>();

		  using (mocks.Record())
		  {
		  	Expect.Call(factory.CreateUser("khurwitz@hotmail.com", "clearTextPassword", "firstName", "lastName")).Return(user);
		  	repository.Save(user);
				Expect.Call(loginService.Login("khurwitz@hotmail.com", "clearTextPassword", true, userRepository)).Return("Login feedback");
		  }

		  using (mocks.Playback())
		  {
		    INewUserService newUserService = new NewUserService(factory, repository, loginService);
		  	string feedback = newUserService.AddNewUser("khurwitz@hotmail.com", "clearTextPassword", "firstName", "lastName", true, userRepository);

		  	Assert.That(feedback, Is.EqualTo("Login feedback"));
		  }
		}
	}
}